package com.swg.seckill.controller;



import com.swg.seckill.pojo.SeckillOrder;
import com.swg.seckill.pojo.User;
import com.swg.seckill.pojo.seckillMessage;
import com.swg.seckill.rabbitmq.MQSender;
import com.swg.seckill.service.IGoodsService;
import com.swg.seckill.utils.JacksonUtil;
import com.swg.seckill.utils.vo.GoodsVo;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author swg
 * @since 2021-11-23
 */
@RestController
@RequestMapping("/seckill-goods")
public class SeckillGoodsController implements InitializingBean {

    @Autowired
    private IGoodsService iGoodsService;

    @Autowired
    private MQSender mqSender;

    @Autowired
    @Qualifier("myRedisTemplate")
    private RedisTemplate redisTemplate;
    //用于标记该商品库存是否已经为0
    private Map<Long,Boolean> EmptyStockMap = new HashMap<>();

    @RequestMapping("/goodsId/{id}")
    public String doSeckill(User user,@PathVariable("id") Long goodsId){
        System.out.println(user.toString());
        if(user == null){
            return "请登录";
        }
        ValueOperations valueOperations = redisTemplate.opsForValue();
        //判断是否重复抢购
        SeckillOrder seckillOrder = (SeckillOrder) redisTemplate.opsForValue().get("order:" + user.getId() + ":" + goodsId);
        if(seckillOrder != null){
            return "you are have";
        }
        //内存标记,减少Redis的访问
        if(EmptyStockMap.get(goodsId)){
            return "not enough";
        }
        //redis预减库存
        Long stock = valueOperations.decrement("seckillGoods" + goodsId);
        if(stock < 0){
            //库存不足直接标记为true,就不用再次来访问了
            EmptyStockMap.put(goodsId,true);
            valueOperations.increment("seckillGoods"+goodsId);
            return "not enough";
        }
        seckillMessage seckillMessage = new seckillMessage(user, goodsId);
        mqSender.sendKillMessage(JacksonUtil.toJson(seckillMessage));

        return seckillMessage.toString();
    }

    /**
     * 系统初始化,把商品库存数量加载到redis
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        List<GoodsVo> goods = iGoodsService.getGoods();
        if(CollectionUtils.isEmpty(goods)){
            return;
        }
        goods.forEach(goodsVo -> {
            redisTemplate.opsForValue().set("seckillGoods" + goodsVo.getId(), goodsVo.getGoodsStock());
            EmptyStockMap.put(goodsVo.getId(),false);
        }
        );
    }
}
